<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class GroupController extends AdminController{

	public function index(){
		$list = $this->getList('id,name,remark', 'group', null, 'id desc', true);
		$this->assign('list', $list);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			$_POST['rules'] = implode(',', $_POST['rules']);
			$result = $this->updateData($_POST, 'group');
			$this->returnResult($result);
		}else{
			$list = $this->getList('id,pid,title', 'node', 'status=1', 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);
			$this->display();
		}
	}

	public function edit(){
		if(IS_POST){
			$_POST['rules'] = implode(',', $_POST['rules']);
			$result = $this->updateData($_POST, 'group', 2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('id,name,remark,rules', 'group', 'id=' . I('get.id', 0, 'intval'));
			$info['rules'] = explode(',', $info['rules']);
			$this->assign('info', $info);			
			$this->display('add');
		}
	}

	/**
	 * 系统模块权限
	 * @return [type] [description]
	 */
	public function jd_quanxian(){
		if(IS_POST){
			$_POST['rules'] = implode(',', $_POST['rules']);
			$result = $this->updateData($_POST, 'group', 2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('id,name,rules', 'group', 'id=' . I('get.id', 0, 'intval'));
			$info['rules'] = explode(',', $info['rules']);
			$this->assign('info', $info);
			
			$list = $this->getList('id,pid,title', 'node', 'status=1', 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);

			$this->display();
		}
	}
	/**
	 * 信息栏目权限
	 * @return [type] [description]
	 */
	public function xx_quanxian(){
		if(IS_POST){
			$_POST['new_cls'] = implode(',', $_POST['new_cls']);
			$result = $this->updateData($_POST, 'group', 2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('id,name,new_cls', 'group', 'id=' . I('get.id', 0, 'intval'));
			$info['new_cls'] = explode(',', $info['new_cls']);
			$this->assign('info', $info);

			$list = $this->getList('id,pid,name', 'classify', 'status=1', 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);

			$this->display();
		}
	}
	public function del(){
		$id = I('get.id', 0, 'intval');
		if($id == 1){
			$this->returnResult(false, array('', '默认用户组不可删除'));
		}
		$num = M('admins')->where(array('gid' => $id))->count();
		if($num > 0){
			$this->returnResult(false, array('', '当前用户组内有用户存在，不可删除'));
		}
		$result = $this->deleteData('id=' . $id, 'group');
		$this->returnResult($result);
	}
}